import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '../layout/layout.vue'
import LoginView from '../views/LoginView.vue'
import ManageView from '../views/childViews/ManageView.vue'
import IndexView from '../views/childViews/IndexView.vue'
import AboutMeView from '../views/childViews/AboutMeView.vue'
import MessageView from '../views/childViews/MessageView.vue'
import SearchView from '../views/childViews/SearchView.vue'
import songView from '../views/childViews/manageView/songView'
import msgView from '../views/childViews/manageView/msgView.vue'
import slideView from '../views/childViews/manageView/slideView'
import userView from '../views/childViews/manageView/userView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView,
    meta:{
      title:'Bibin | 登录'
    }
  },
  {
    path: '/index',
    name: 'layout',
    component: layout,
    meta:{
      title:'Bibin | 首页'
    },
    children: [
      {
        path: '/manage',
        name: 'manage',
        component: ManageView,
        meta: {
          title: 'Bibin | 后台'
        },
        children:[
            {
              path: '/song',
              name: 'song',
              component: songView,
            },
            {
              path: '/msg',
              name: 'msg',
              component: msgView,
            },
            {
              path: '/slide',
              name: 'slide',
              component: slideView,
            },
            {
              path: '/user',
              name: 'user',
              component: userView,
            },
        ]
      },
      {
        path: '/indexView',
        name: 'indexView',
        component: IndexView,
        meta:{
          title:'Bibin | 首页'
        }
      },
      {
        path: '/message',
        name: 'message',
        component: MessageView,
        meta:{
          title:'Bibin | 留言板'
        }
      },
      {
        path: '/search',
        name: 'search',
        component: SearchView,
        meta:{
          title:'Bibin | 搜索'
        }
      },
      {
        path: '/aboutMe',
        name: 'aboutMe',
        component: AboutMeView,
        meta:{
          title:'Bibin | 关于作者'
        }
      },
  ],
  
  }
]

const router = new VueRouter({
  mode:'history',
  routes
})

router.beforeEach((to, from, next) =>{
  const token = localStorage.getItem('token')
  // debugger
  if(to.path ==="/"){
    next()
  }else{
    if(!token){
      next("/")
    }else{
      next()
    }
  }
})


export default router
